package com.network;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

import com.util.FileUtil;

/**
 * @ClassName: HttpDataTransfer
 * @Description: http发送数据
 * @author yanghao
 * @date 2016年10月27日 上午10:37:44
 */
public class HttpTransfer {

	/**
	 * @Title: buildHttpConn
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param httpUrl
	 * @param @return
	 * @param @throws IOException 设定文件
	 * @return HttpURLConnection 返回类型
	 * @throws
	 */
	private HttpURLConnection buildHttpConn(String httpUrl) throws IOException {
		Integer timeout = 1000 * 60 * 10;
		URL url = new URL(httpUrl);
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		urlConn.setDoOutput(true);
		urlConn.setDoInput(true);
		urlConn.setRequestMethod("POST");
		urlConn.setConnectTimeout(timeout);
		urlConn.setRequestProperty("Content-type", "text/html");
		urlConn.setRequestProperty("Accept-Charset", "utf-8");
		urlConn.setRequestProperty("contentType", "utf-8");
		return urlConn;
	}

	/**
	 * @Title: transfer
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param httpUrl
	 * @param @param json
	 * @param @return
	 * @param @throws Exception 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public String transfer(String httpUrl, String json) throws Exception {
		return transfer(httpUrl, json, null);
	}

	/**
	 * @Title: transfer
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param httpUrl
	 * @param @param json
	 * @param @param prams
	 * @param @return
	 * @param @throws Exception 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public String sendJson(String httpUrl, String json, Map<String, String> prams) throws Exception {
		OutputStream out = null;
		InputStream inFile = null;
		InputStream in = null;
		BufferedReader reader = null;
		HttpURLConnection urlConn = null;
		try {
			urlConn = buildHttpConn(httpUrl);
			// 参数
			if (prams != null && prams.size() > 0) {
				Set<String> keys = prams.keySet();
				for (String key : keys) {
					urlConn.setRequestProperty(key, prams.get(key));
				}
			}
			out = urlConn.getOutputStream();
			inFile = new ByteArrayInputStream(json.getBytes("UTF-8"));
			int len = -1;
			byte[] bt = new byte[2048];
			while ((len = inFile.read(bt)) != -1) {
				out.write(bt, 0, len);
			}
			out.flush();
			if (urlConn.getResponseCode() != 200) {
				throw new Exception("http返回失败，错误码" + urlConn.getResponseCode());
			}
			in = urlConn.getInputStream();
			reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			StringBuffer bf = new StringBuffer();
			String temp = "";
			while ((temp = reader.readLine()) != null) {
				bf.append(temp);
			}
			return bf.toString();
		} catch (SocketTimeoutException e) {
			throw new Exception("连接超时 ", e);
		} catch (ConnectException e) {
			throw new Exception("无法连接 ", e);
		} catch (IOException e) {
			throw new Exception("文件传输失败 ", e);
		} finally {
			/* 关闭流 */
			FileUtil.close(out);
			FileUtil.close(inFile);
			FileUtil.close(reader);
			FileUtil.close(in);
			if (urlConn != null) {
				urlConn.disconnect();
			}
		}
	}

	/**
	 * @Title: test
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param httpUrl
	 * @param @return
	 * @param @throws Exception 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public String test(String httpUrl) throws Exception {
		return transfer(httpUrl, "");
	}

}
